package com.intellekt.ecertws.ws.service;
/*
* This software is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
*/
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.intellekt.ecertws.ws.dao.CertDao;
import com.intellekt.ecertws.ws.dao.CertDaoImpl;
import com.intellekt.ecertws.ws.vo.UserVO;


public class AuthenticationServiceImpl implements UserDetailsService {

	private static final Logger log = Logger.getLogger(AuthenticationServiceImpl.class);
	
	@Autowired
	private CertDao dao;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		return verify(username);
	}

	
	public UserDetails verify(String username){
		if(log.isDebugEnabled()){
			log.debug("getUser "+username);
		}
		UserVO vo = dao.getUser(username);
		
		if(log.isDebugEnabled()){
			log.debug("username..."+username);
			log.debug("username..."+vo.getUsername());
			log.debug("password..."+vo.getPassword());
		}
		
		if(vo == null)
			throw new  UsernameNotFoundException("No existe el usuario");
		
		//UserVO mydetail = new UserVO(username,"pass",true,true,true,true);
		//mydetail.getAuthorities().add(new SimpleGrantedAuthority("ROLE_USER"));
		
		return vo;
	}
}
